﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class LevelCubeLove : MonoBehaviour
{
    public List<Button> btnList = new List<Button>();
    public List<GameObject> loveRootList = new List<GameObject>();
    public List<GameObject> btnImageList = new List<GameObject>();
    private List<int> bigIndexList = new List<int>();
    private string curStr = string.Empty;
    private string targetStr = "LOVE";

    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        InitBtnEvent();
    }

    private void InitBtnEvent()
    {
        int count = btnList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            btnList[i].onClick.AddListener(() =>
            {
                SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
                if (bigIndexList.Contains(tempIndex))
                    return;
                bigIndexList.Add(tempIndex);
                btnImageList[tempIndex].transform.DOScale(1.5f, 0.2f);
                curStr += btnList[tempIndex].name;
                JudgeResult();
            });
        }
    }

    private void JudgeResult()
    {
        if (curStr.Equals(targetStr))
        {
            Invoke("WinAni", 0.5f);
        }
    }

    private void WinAni()
    {
        for (int i = 0; i < bigIndexList.Count; i++)
        {
            btnList[bigIndexList[i]].transform.parent = this.transform;
            btnList[bigIndexList[i]].transform.DOMove(loveRootList[i].transform.position, 0.2f);
        }

        Invoke("TriggerWin", 0.5f);
    }

    private void TriggerWin()
    {
        gameLevelItem.TriggerResult(true);
    }
}
